package com.xuxin.baby.popup;

import android.content.Context;
import android.content.res.AssetManager;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.CenterPopupView;
import com.xuxin.baby.R;
import com.xuxin.baby.bean.CityListBean;
import com.xuxin.baby.utils.RecycleUtils;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by xu_xin
 * Created on 2020/11/26 0026
 * 城市选择器
 */
public class CityPopup extends CenterPopupView {
    private CityListBean cityListBean;
    private CityChildAdapter cityChildAdapter;
    private OnClickListener onClickListener = null;

    public CityPopup(@NonNull Context context) {
        super(context);
    }

    public CityPopup(@NonNull Context context, OnClickListener onClickListeners) {
        super(context);
        onClickListener = onClickListeners;
    }

    public interface OnClickListener {
        void onCity(String city);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_city;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        cityListBean = GsonUtils.fromJson(getJson(getContext()), CityListBean.class);
        RecyclerView popup_city = findViewById(R.id.popup_city);
        RecycleUtils.init(popup_city);
        CityAdapter cityAdapter = new CityAdapter();
        popup_city.setAdapter(cityAdapter);
        cityAdapter.setList(cityListBean.getChina());
        cityAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                try {
                    cityChildAdapter.setList(cityListBean.getChina().get(position).getCity());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        RecyclerView popup_city_child = findViewById(R.id.popup_city_child);
        RecycleUtils.init(popup_city_child);
        cityChildAdapter = new CityChildAdapter();
        popup_city_child.setAdapter(cityChildAdapter);
        cityChildAdapter.setList(cityListBean.getChina().get(0).getCity());
        cityChildAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (null != onClickListener) {
                    try {
                        onClickListener.onCity(cityChildAdapter.getData().get(position).getName());
                        dismiss();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    protected int getMaxWidth() {
        return (int) (ScreenUtils.getScreenWidth() * 0.75);
    }

    @Override
    protected int getMaxHeight() {
        return (int) (ScreenUtils.getScreenWidth());
    }

    // 设置自定义动画器，看需要而定
    @Override
    protected PopupAnimator getPopupAnimator() {
        return super.getPopupAnimator();
    }

    protected int getPopupWidth() {
        return 0;
    }

    protected int getPopupHeight() {
        return 0;
    }

    public static class CityAdapter extends BaseQuickAdapter<CityListBean.ChinaBean, BaseViewHolder> {

        public CityAdapter() {
            super(R.layout.item_city);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, CityListBean.ChinaBean itemsBean) {
            holder.setText(R.id.item_city_name, itemsBean.getName());
        }
    }

    public static class CityChildAdapter extends BaseQuickAdapter<CityListBean.ChinaBean.CityBean, BaseViewHolder> {

        public CityChildAdapter() {
            super(R.layout.item_city);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, CityListBean.ChinaBean.CityBean itemsBean) {
            holder.setText(R.id.item_city_name, itemsBean.getName());
        }
    }

    private static String getJson(Context context) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open("city.json")));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}